package com.example.administrator.yanfoxconn.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeDateUtils {

    private static SimpleDateFormat formatters = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private static SimpleDateFormat formatterDate = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat formatterTime = new SimpleDateFormat("HH:mm");

    private static int day;//相差天數
    private static int hour;//相差小時數
    //相差天數
    public static int daysDeviation(String dateStart, String dateEnd) {

        try {
            Date startDate = formatters.parse(dateStart);
            Date endDate = formatters.parse(dateEnd);
            //这样得到的差值是微秒级别
            long diff = endDate.getTime() - startDate.getTime();

            long days = diff / (1000 * 60 * 60 * 24);

//            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
//
//            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
//
//            System.out.println(""+days+"天"+hours+"小时"+minutes+"分");

            day = (int) days;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return day;
        }
    }
    //相差天數
    public static int daysDeviationDate(String dateStart, String dateEnd) {

        try {
            Date startDate = formatterDate.parse(dateStart);
            Date endDate = formatterDate.parse(dateEnd);
            //这样得到的差值是微秒级别
            long diff = endDate.getTime() - startDate.getTime();

            long days = diff / (1000 * 60 * 60 * 24);

//            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
//
//            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
//
//            System.out.println(""+days+"天"+hours+"小时"+minutes+"分");

            day = (int) days;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return day+1;//包含start，end當天
        }
    }
    //相差小時數
    public static int hoursDeviation(String dateStart, String dateEnd) {

        try {
            Date startDate = formatters.parse(dateStart);
            Date endDate = formatters.parse(dateEnd);

            long diff = endDate.getTime() - startDate.getTime();//这样得到的差值是微秒级别

//            long days = diff / (1000 * 60 * 60 * 24);
//
//            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
//
//            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
//
//            System.out.println(""+days+"天"+hours+"小时"+minutes+"分");
            long hours = diff / (1000 * 60 * 60);
            hour = (int) hours;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return hour;
        }
    }
    //相差小時數
    public static int hoursDeviationTime(String dateStart, String dateEnd) {

        try {
            Date startDate = formatterTime.parse(dateStart);
            Date endDate = formatterTime.parse(dateEnd);

            long diff = endDate.getTime() - startDate.getTime();//这样得到的差值是微秒级别

//            long days = diff / (1000 * 60 * 60 * 24);
//
//            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
//
//            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
//
//            System.out.println(""+days+"天"+hours+"小时"+minutes+"分");
            long hours = diff / (1000 * 60 * 60);
            hour = (int) hours;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return hour;
        }
    }
    //相差分鐘數
    public static int minutesDeviation(String dateStart, String dateEnd) {

        try {
            Date startDate = formatterTime.parse(dateStart);
            Date endDate = formatterTime.parse(dateEnd);

            long diff = endDate.getTime() - startDate.getTime();//这样得到的差值是微秒级别

//            long days = diff / (1000 * 60 * 60 * 24);
//
//            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
//
//            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
//
//            System.out.println(""+days+"天"+hours+"小时"+minutes+"分");
            long hours = diff / (1000 * 60 );
            hour = (int) hours;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return hour;
        }
    }
    //相差秒數
    public static int secondDeviation(String dateStart, String dateEnd) {

        try {
            Date startDate = formatters.parse(dateStart);
            Date endDate = formatters.parse(dateEnd);

            long diff = endDate.getTime() - startDate.getTime();//这样得到的差值是微秒级别

//          long days = diff / (1000 * 60 * 60 * 24);
//          long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
//          long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
//          System.out.println(""+days+"天"+hours+"小时"+minutes+"分");

            long hours = diff / (1000);
            hour = (int) hours;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return hour;
        }
    }

    /**
     * @param strH   開始時間的 小時
     * @param strM   開始時間的 分鐘
     * @param endH   結束時間的 小時
     * @param endM   結束時間的 分鐘
     * @return
     */
    public static boolean isContain(int strH,int strM,int endH,int endM){
        Boolean timeRight = false;
        SimpleDateFormat formatters = new SimpleDateFormat("HH:mm");
        Date curDates = new Date(System.currentTimeMillis());// 获取当前时间
        String strs = formatters.format(curDates);
//            System.out.println(strs);
        //开始时间
        int sth = strH;//小时
        int stm = strM;//分
        //结束时间
        int eth = endH;//小时
        int etm = endM;//分

        String[] dds = new String[]{};

        // 分取系统时间 小时分
        dds = strs.split(":");
        int dhs = Integer.parseInt(dds[0]);
        int dms = Integer.parseInt(dds[1]);

        if (sth <= dhs && dhs <= eth) {
            if (sth == dhs && stm <= dms) {
//                    System.out.println("在外围内");
                timeRight = true;
            } else if (dhs == eth && etm >= dms) {
//                    System.out.println("在外围内");
                timeRight = true;
            } else if (sth != dhs && dhs != eth) {
//                    System.out.println("在外围內");
                timeRight = true;
            }
        }

        return timeRight;
    }

    /**
     * 比較日期大小
     * @param startTime
     * @param endTime
     * @return
     */
    public static int getDateCompareSize(String startTime, String endTime){
        int i=0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//年-月-日
        try {
            Date date1 = dateFormat.parse(startTime);//开始时间
            Date date2 = dateFormat.parse(endTime);//结束时间
            // 1 结束时间小于开始时间 2 开始时间与结束时间相同 3 结束时间大于开始时间
            if (date2.getTime()<date1.getTime()){
                i= 1;
            }else if (date2.getTime()==date1.getTime()){
                i= 2;
            }else if (date2.getTime()>date1.getTime()){
                //正常情况下的逻辑操作.
                i= 3;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  i;
    }

    /**
     * 判断时间大小:小于和等于为true
     * @param startTime
     * @param endTime
     * @return
     */
    public static Boolean getTimeLessEqual (String startTime, String endTime) throws ParseException {
Boolean t =null;
        Log.e("------",startTime+"===="+endTime);
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");//时-分

            Date date1 = dateFormat.parse(startTime);//开始时间
            Date date2 = dateFormat.parse(endTime);//结束时间

        Log.e("------",date1.toString()+"==Date=="+date2.toString());
            if (date1.getTime()<date2.getTime()){
                t = true;
            }else if (date1.getTime()==date2.getTime()){
                t = true;
            }else {
                t = false;
            }

        return t;
    }
    /**
     * 判断时间大小:大于为true
     * @param startTime
     * @param endTime
     * @return
     */
    public static Boolean getTimeGreater (String startTime, String endTime){

        Log.e("------",startTime+"===="+startTime);
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");// 时-分
        try {
            Date date1 = dateFormat.parse(startTime);//开始时间
            Date date2 = dateFormat.parse(endTime);//结束时间
            // 1 结束时间小于开始时间 2 开始时间与结束时间相同 3 结束时间大于开始时间
            if (date1.getTime()>date2.getTime()){
                return true;
            }else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 计算相差小时数
     * @param dateStart
     * @param dateEnd
     * @return
     */
    private static float hou;
    public static float getTimeHours(String dateStart, String dateEnd) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");// 时-分
        try {
            Date startDate = dateFormat.parse(dateStart);
            Date endDate = dateFormat.parse(dateEnd);
            float diff = endDate.getTime() - startDate.getTime();//这样得到的差值是微秒级别
            float hours = diff / (1000 * 60 * 60);
            hou = (float) hours;
            Log.e("--------","hou===="+hou);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            return hou;
        }
    }
    /**
     196   * 获取现在时间
     197   *
     198   * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
     199   */
  public static Date getNowDate() {
          Date insertDate = null;
      SimpleDateFormat simpleFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      String datastring=simpleFormat.format(new Date());
//把获取的时间转换为date格式
      try {
      insertDate=simpleFormat.parse(datastring);
      } catch (ParseException e) {
          e.printStackTrace();
      }
      return insertDate;
  }
    //string轉date
    public static Date stringToDate(String str){
        Date currentTime_1 = null;
        try{
            System.out.println("currentTime_1=====："+currentTime_1);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          currentTime_1 = sdf.parse(sdf.format(str));
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("currentTime_2=====："+currentTime_1);
            return currentTime_1;
        }
    }

    public static int dateDiff(Date startTime, Date endTime) {
//按照传入的格式生成一个simpledateformate对象
        long nd = 1000*24*60*60;//一天的毫秒数
        long nh = 1000*60*60;//一小时的毫秒数
        long nm = 1000*60;//一分钟的毫秒数
        long ns = 1000;//一秒钟的毫秒数long diff;try {
//获得两个时间的毫秒时间差异
        long diff = endTime.getTime() - startTime.getTime();
        long day = diff/nd;//计算差多少天
        long hour = diff/nd/nh;//计算差多少小时
        long min = diff/nm;//计算差多少分钟
        long sec = diff%nd%nh%nm/ns;//计算差多少秒//输出结果
        System.out.println("时间相差："+day+"天"+hour+"小时"+min+"分钟"+sec+"秒。");
        return (int) min;
}
//獲取相差月份
    public static int monthCha(String startTime, String endTime) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");// 时-分
        int monthsDiff=0;
        try {
            Date startDate = dateFormat.parse(startTime);
            Date endDate = dateFormat.parse(endTime);
            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();
            start.setTime(startDate);
            end.setTime(endDate);
            int yearS = start.get(Calendar.YEAR);
            int yearE = end.get(Calendar.YEAR);
            int monthS = start.get(Calendar.MONTH);
            int monthE = end.get(Calendar.MONTH);
            int dayS = start.get(Calendar.DAY_OF_MONTH);
            int dayE = end.get(Calendar.DAY_OF_MONTH);
            //獲取年差值
            int yearInterval = yearS - yearE;
             monthsDiff = (yearInterval * 12 + monthS-monthE);
             if (dayS>dayE){
                 monthsDiff=monthsDiff+1;
             }
            return monthsDiff;
        } catch (ParseException e) {
            e.printStackTrace();
        }finally {

        return monthsDiff;}
    }

    public static Calendar stringToCalendar(String sdate){
        //String转Date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //Date转Calendar
        Calendar calendar = Calendar.getInstance();
        try {
            Date date = sdf.parse(sdate);
        calendar.setTime(date);
        System.out.println("date="+date+", calendar="+calendar);
        return calendar;
        } catch (ParseException e) {
            e.printStackTrace();
        }finally {
            return calendar;
        }
    }
}
